package com.lf.api.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.lf.api.po.dao.SubjectMapper;
import com.lf.api.po.dao.UserCommentMapper;
import com.lf.api.po.dao.UserGreateMapper;
import com.lf.api.po.dao.UserInfoMapper;
import com.lf.api.po.dao.UserReplyMapper;
import com.lf.api.po.domain.UserInfo;
import com.lf.api.service.UserService;
import com.lf.api.vo.MessageResult;
import com.lf.api.weixin.JacksonUtil;
import com.lf.common.util.CommonUtil;
import com.lf.common.util.UuidUtils;

/**
 * 
 * Description: 用户业务类：用户注册，用户登陆
 * Copyright: Copyright (c)2012
 * Company: 小小书童
 * @author: 小小书童
 * @version: 1.0
 * Create at: 2016年9月17日 上午12:46:27
 *
 * Modification History:
 * Date Author Version Description
 * ---------------------------修改历史---------------------------------------
 * 2016年9月17日 小小书童 1.0
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	UserCommentMapper userCommentMapper;
	@Autowired
	UserReplyMapper userReplyMapper;
	@Autowired
	SubjectMapper subjectMapper;
	@Autowired
	UserInfoMapper userInfoMapper;
	@Autowired
	UserGreateMapper userGreateMapper;
	@Autowired
	RedisTemplate<String, String> redisTemplate;
	/**
	 * Description:用户注册业务方法
	 * 0-注册用户成功；1-参数传输出不正确；2-用户名为空；3-密码为空；4-用户名已存在
	 * @param user
	 * @return
	 * @see com.lf.api.service.UserService#registerUser(com.lf.api.po.domain.UserInfo)
	 */
	@Override
	public MessageResult register(UserInfo user) {
		if (null == user) {
			return MessageResult.SYSTEM_ERROR.setCode(1);
		}
		if(StringUtils.isEmpty(user.getUsername()) || !CommonUtil.isMobile(user.getUsername())){
			return MessageResult.SYSTEM_ERROR.setCode(2);
		}
		if(StringUtils.isEmpty(user.getPassword())){
			return MessageResult.SYSTEM_ERROR.setCode(3);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", user.getUsername());
		List<UserInfo> userInfoList = userInfoMapper.queryList(map);
		if (CollectionUtils.isEmpty(userInfoList)) {
			return MessageResult.SYSTEM_ERROR.setCode(4);
		}
		completeUserInfo(user);
		userInfoMapper.insert(user);
		return MessageResult.SUCCESS;
	}

	/** 补全用户信息 */
	public void completeUserInfo(UserInfo user) {
		if (null == user)
			return;
		try {
			user.setSalt(CommonUtil.randomNumbers());
			user.setPassword(CommonUtil.shaEncode(user.getPassword(), user.getSalt()));
			user.setLevel((short) 0);
			user.setSpreadCode(UuidUtils.compressedUuid());
			user.setState((short) 0);
			user.setGender((short) 0);
			user.setFansNum(0);
			user.setFavoriteNum(0);
			user.setSubscribeNum(0);
			user.setSubjectNum(0);
			user.setCreateTime(new Date());
			user.setScore(100);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 0-登陆成功；1-参数传输出不正确；2-用户名为空；3-密码为空；4-用户不存在；5-密码不正确；6-用户被禁止登陆
	 * @param user
	 * @return
	 * @throws Exception 
	 * @see com.lf.api.service.UserService#registerUser(com.lf.api.po.domain.UserInfo)
	 */
	@Override
	public MessageResult login(String username, String password) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		List<UserInfo> users = selectUser(map);
		if(CollectionUtils.isEmpty(users)){
			return MessageResult.SYSTEM_ERROR.setMsg("username is not exist!");
		}
		UserInfo user = users.get(0);
		String encodePwd = CommonUtil.shaEncode(password, user.getSalt());
		if(!password.equals(encodePwd)){
			return MessageResult.SYSTEM_ERROR.setMsg("passwrod is incorrect!");
		}
		String token = UuidUtils.compressedUuid();
		redisTemplate.opsForValue().set(token, JacksonUtil.bean2JSon(user));
		redisTemplate.opsForValue().set(user.getUsername(), token);
		return MessageResult.SUCCESS.setMsg(token);
	}

	@Override
	public List<UserInfo> selectUser(Map<String, Object> map) {
		return userInfoMapper.queryList(map);
	}

	@Override
	public void insertUser(UserInfo user) {
		userInfoMapper.insert(user);
	}

}
